//
//  HBCompleteBankInfoCtrl.m
//  HuoBao
//
//  Created by sky on 15/12/8.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBCompleteBankInfoCtrl.h"

#import "HBSetAccountBankParam.h"
#import "HBSetAccountBankParser.h"
#import "HBUserDistillParam.h"
#import "HBUserDistillParser.h"
#import "CheckValid.h"
#import "HBWithdrawCtrl.h"

#import "HBGetBankServicePhoneParam.h"
#import "HBGetBankServicePhoneParser.h"
#import "HBGetBankServicePhoneInfo.h"

#import "CustomNoTradingPwdAlert.h"
#import "HBSetTradpasswordCtrl.h"


@interface HBCompleteBankInfoCtrl ()<UITextFieldDelegate>
{
    UILabel *_title;
    UITextField *_bankname;
    UIButton *_surebtn;
    UILabel *_tishi;
    UILabel *_content;
}

@end

@implementation HBCompleteBankInfoCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)buildLayout{
    self.title = @"开户行信息";
    [self addTap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    UIView *firvi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    firvi.backgroundColor = [LCCommon getColor:@"323232"];
    [self.view addSubview:firvi];
    
    
    _title = [self LabelWithRect:CGRectMake(15, 15 - 6, SCREEN_WIDTH/2, 12) textColor:[LCCommon getColor:@"919191"] textstr:@"已绑定招商银行卡(0245)" textfont:12 textstyle:NSTextAlignmentLeft];
    [firvi addSubview:_title];
    
    [self makeLinewithRect:CGRectMake(15, 30 + 20, SCREEN_WIDTH - 30, .5) Color:[LCCommon getColor:@"585858"] addview:self.view];
    
    [self makeLinewithRect:CGRectMake(15, 50 + 44, SCREEN_WIDTH - 30, .5) Color:[LCCommon getColor:@"585858"] addview:self.view];
    
    
    
    UILabel *kaihu = [self LabelWithRect:CGRectMake(15, 50, 16 * 3, 44) textColor:[LCCommon getColor:@"b1b1b1"] textstr:@"开户行" textfont:16 textstyle:NSTextAlignmentCenter];
    [self.view addSubview:kaihu];
    
    
    _bankname = [self TextFieldWithRect:CGRectMake(kaihu.origin.x + 3 * 16 + 50 , kaihu.origin.y, SCREEN_WIDTH - 30 - (kaihu.origin.x + 3 * 16 + 50) ,44) leftImage:nil placeHold:@"请输入开户行名称" index:1];
    [self.view addSubview:_bankname];
    
    
    _surebtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 50 + 44 + 110/2, SCREEN_WIDTH - 30, 45)];
    [_surebtn setBackgroundColor:[LCCommon getColor:@"f24545"] forState:UIControlStateNormal];
    [_surebtn setBackgroundColor:[LCCommon getColor:@"bd3737"] forState:UIControlStateDisabled];
    _surebtn.enabled = NO;
    [_surebtn setTitle:@"确定" forState:UIControlStateNormal];
    _surebtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [_surebtn setTitleColor:[LCCommon getColor:@"ffffff"] forState:UIControlStateNormal];
    [_surebtn setTitleColor:[LCCommon getColor:@"fd7a7a"] forState:UIControlStateDisabled];
    _surebtn.clipsToBounds = YES;
    _surebtn.layer.cornerRadius = 4;
    [_surebtn addTarget:self action:@selector(btntoSure) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_surebtn];
    
    UILabel *tishi = [self LabelWithRect:CGRectMake(15, _surebtn.origin.y + 45 + 30, SCREEN_WIDTH/2, 16) textColor:[LCCommon getColor:@"b1b1b1"] textstr:@"温馨提示" textfont:16 textstyle:NSTextAlignmentLeft];
    [self.view addSubview:tishi];
    _tishi = tishi;
    
    NSString *str = @"       根据银行规定,首次提醒操作需对您已绑定的尾号为0000的某某银行卡的开户行进行验证,再次提现无需验证。为了避免提现失败给您造成不必要的麻烦,请您务必填写正确的开户行名称,如您不确定该卡开户行名称,请拨打某某银行客服电话0000咨询确认后在填写。谢谢您的配合!";
    UILabel *contemt = [self LabelWithRect:CGRectMake(15, tishi.origin.y + 16 + 16, SCREEN_WIDTH - 30, 0) textColor:[LCCommon getColor:@"b1b1b1"] textstr:str textfont:14 textstyle:NSTextAlignmentLeft];
    [contemt sizeToFit];
    [self.view addSubview:contemt];
    _content = contemt;
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:str];
    NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc]init];
    [para setLineSpacing:7];
    [attr addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0, [str length])];
    contemt.attributedText = attr;
    [contemt sizeToFit];
    
    _title.text = [NSString stringWithFormat:@"已绑定%@卡(%@)",self.userDistillInfo.bankName,self.userDistillInfo.bankCardShort];
    
    [self requestToGetBankInfo];
}


-(void)doTouch:(UIGestureRecognizer *)sender{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textColor = color;
    label.text = str;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}


- (UITextField *)TextFieldWithRect:(CGRect)rect leftImage:(UIImage *)image placeHold:(NSString *)placeHold index:(NSInteger)index
{
    UITextField *text = [[UITextField alloc] initWithFrame:rect];
    text.placeholder = placeHold;
    text.backgroundColor = [UIColor clearColor];
    text.delegate = self;
    text.font = [UIFont systemFontOfSize:14];
    text.textColor = [LCCommon getColor:@"ffffff"];
    [text setValue:[LCCommon getColor:@"585858"] forKeyPath:@"_placeholderLabel.textColor"];
    
    
    [self.view addSubview:text];
    //    UIView *line = [[UIView alloc]init];
    //    if (index == 1) {
    //        line = [[UIView alloc] initWithFrame:CGRectMake(40, rect.size.height + rect.origin.y + 2, rect.size.width, .5)];
    //    }else{
    //        line = [[UIView alloc] initWithFrame:CGRectMake(40, rect.size.height + rect.origin.y + 2, SCREEN_WIDTH - 80, .5)];
    //    }
    //
    //    line.backgroundColor = [LCCommon getColor:@"a8a8a8"];
    //    [_bgview addSubview:line];
    //    line = nil;
    return text;
}


-(UIView *)makeLinewithRect:(CGRect)rect Color:(UIColor *)color addview:(UIView *)vi{
    UIView *line = [[UIView alloc]initWithFrame:rect];
    line.backgroundColor = color;
    [vi addSubview:line];
    return vi;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)textDidChange{
    if (_bankname.text.length > 0) {
        _surebtn.enabled = YES;
    }else{
        _surebtn.enabled = NO;
    }
}


-(void)btntoSure{
    LLog(@"btntoDraw");
    if ([CheckValid checkStrIsEmpty:_bankname.text])
    {
        DISSMISS_ERR(@"开户行不能为空");
        
    }
    else if ([CheckValid isIncludeEmoji:_bankname.text])
    {
        DISSMISS_ERR(@"请输入正确的开户行");
    }
    else
    {
        [_bankname resignFirstResponder];
        [self requestToBindBank];
    }
}

-(void)requestToBindBank{
    //    HBCompleteBankInfoCtrl *draw = [[HBCompleteBankInfoCtrl alloc]init];
    //    [self.navigationController pushViewController:draw animated:YES];
    
//    [CustomLoadingView showHudToView:nil];
    [CustomLoadingView showHudToView:self.view];
    HBSetAccountBankParam *param = [[HBSetAccountBankParam alloc]init];
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.accountBank = _bankname.text;
    HBSetAccountBankParser *parser = [[HBSetAccountBankParser alloc]init];
    [NetCenter postDataFromURL:Method_setAccountBank params:param parser:parser result:^(ParserResult *myResult) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code == 0)
        {
//            if(![self.userDistillInfo.tradingPwdStatus integerValue])
//            {
//                CustomNoTradingPwdAlert *alert = [[CustomNoTradingPwdAlert alloc]init];
//                [alert showTradPasswordAlertView];
//                alert.ActionClickToSetTradingPwd = ^(CustomNoTradingPwdAlert *alertP)
//                {
//                    [alertP removeFromSuperview];
//                    HBSetTradpasswordCtrl *addBankCtrl = [[HBSetTradpasswordCtrl alloc]initWithNibName:NSStringFromClass([HBSetTradpasswordCtrl class]) bundle:nil];
//                    addBankCtrl.isFromTiXian = YES;
//                    [self.navigationController pushViewController:addBankCtrl animated:YES];
//                };
//                __weak typeof(self) weakSelf = self;
//                alert.ActionClickToCancel = ^()
//                {
//                    [weakSelf.navigationController popToRootViewControllerAnimated:YES];
//                };
//            }
//            else
//            {
//                [self requestToGetDistillInfo];
//            }
            
            [self requestToGetDistillInfo];
            
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
    
}

-(void)requestToGetDistillInfo{
    //    HBCompleteBankInfoCtrl *draw = [[HBCompleteBankInfoCtrl alloc]init];
    //    [self.navigationController pushViewController:draw animated:YES];
    
//    [CustomLoadingView showHudToView:nil];
    [CustomLoadingView showHudToView:self.view];
    HBUserDistillParam *param = [[HBUserDistillParam alloc]init];
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    HBUserDistillParser *parser = [[HBUserDistillParser alloc]init];
    [NetCenter postDataFromURL:Method_userDistill params:param parser:parser result:^(ParserResult *myResult) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code == 0)
        {
            HBUserDistillInfo *info = (HBUserDistillInfo *)myResult.result;
            
            if(![info.tradingPwdStatus integerValue])
            {
                CustomNoTradingPwdAlert *alert = [[CustomNoTradingPwdAlert alloc]init];
                [alert showTradPasswordAlertView];
                alert.ActionClickToSetTradingPwd = ^(CustomNoTradingPwdAlert *alertP)
                {
                    [alertP removeFromSuperview];
                    HBSetTradpasswordCtrl *addBankCtrl = [[HBSetTradpasswordCtrl alloc]initWithNibName:NSStringFromClass([HBSetTradpasswordCtrl class]) bundle:nil];
                    addBankCtrl.isFromTiXian = YES;
                    [self.navigationController pushViewController:addBankCtrl animated:YES];
                };
                __weak typeof(self) weakSelf = self;
                alert.ActionClickToCancel = ^()
                {
                    [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                };
            }
            else
            {
                
                HBWithdrawCtrl *expofCtrl = [[HBWithdrawCtrl alloc]init];
                expofCtrl.userDistillInfo = info;
                [self.navigationController pushViewController:expofCtrl animated:YES];
            }
           
            
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
    
}

-(void)requestToGetBankInfo{
    //    HBCompleteBankInfoCtrl *draw = [[HBCompleteBankInfoCtrl alloc]init];
    //    [self.navigationController pushViewController:draw animated:YES];
    
//    [CustomLoadingView showHudToView:nil];
    [CustomLoadingView showHudToView:self.view];
    HBGetBankServicePhoneParam *param = [[HBGetBankServicePhoneParam alloc]init];
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.bankCode = self.userDistillInfo.bankCode;
    HBGetBankServicePhoneParser *parser = [[HBGetBankServicePhoneParser alloc]init];
    [NetCenter getDataFromURL:Method_getBankServicePhone params:param parser:parser result:^(ParserResult *myResult) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code == 0)
        {
            HBGetBankServicePhoneInfo *info = (HBGetBankServicePhoneInfo *)myResult.result;
            
            //                HBExpofGoldCtrl *expofCtrl = [[HBExpofGoldCtrl alloc]init];
            //                expofCtrl.userDistillInfo = info;
            //                [self.navigationController pushViewController:expofCtrl animated:YES];
            
            NSString *str = [NSString stringWithFormat:@"       根据银行规定,首次提醒操作需对您已绑定的尾号为%@的%@卡的开户行进行验证,再次提现无需验证。为了避免提现失败给您造成不必要的麻烦,请您务必填写正确的开户行名称,如您不确定该卡开户行名称,请拨打%@客服电话%@咨询确认后在填写。谢谢您的配合!",self.userDistillInfo.bankCardShort,self.userDistillInfo.bankName,self.userDistillInfo.bankName,info.servicePhone];
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:str];
            NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc]init];
            [para setLineSpacing:7];
            [attr addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0, [str length])];
            _content.attributedText = attr;
            [_content sizeToFit];
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
//        [CustomLoadingView hideHudForView:nil];
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
